/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "StatusDlg.h"
#include "onyx/ui/status_bar_item_power.h"
#include "onyx/ui/power_management_dialog.h"
#include "onyx/ui/status_bar_item_clock.h"
#include "onyx/ui/clock_dialog.h"
#include "MainDialog.h"
#include "BookmarkDlg.h"

extern QDialog* g_mainDialog;

StatusDlg::StatusDlg(CoconProfile& profile, int iPage, int iDivPage, int iPageCount, int disContent,  QWidget* parent) : ui::OnyxDialog(parent)
{
    setFixedSize(400, 280);

    updateTitle(tr("状态信息"));

    m_profile =  &profile;

    m_operation = Oper_Invalid;

    m_mainlayout = new QHBoxLayout(this);
    content_widget_.setLayout(m_mainlayout);

    //-- layout
    QVBoxLayout* layoutStatus = new QVBoxLayout(this);
    QVBoxLayout* layoutButtons = new QVBoxLayout(this);
    m_mainlayout->addLayout(layoutStatus);
    m_mainlayout->addStretch(1);
    m_mainlayout->addLayout(layoutButtons);

    //-- system status
    QHBoxLayout* layoutBar = new QHBoxLayout(this);
    layoutStatus->addLayout(layoutBar);

    ui::StatusBarItemBattery* batteryItem = new ui::StatusBarItemBattery(this);
    connect(batteryItem, SIGNAL(clicked()), this, SLOT(OnBattery()));
    layoutBar->addWidget(batteryItem);
    int batteryValue;
    int batteryStatus;
    sys::SysStatus::instance().batteryStatus(batteryValue, batteryStatus);
    batteryItem->setBatteryStatus(batteryValue, batteryStatus);

    ui::StatusBarItemClock* clockItem = new ui::StatusBarItemClock(this);
    connect(clockItem, SIGNAL(clicked()), this, SLOT(OnClock()));
    layoutBar->addWidget(clockItem);

    layoutBar->addStretch(1);

    //-- file status
    QDir dir(profile.filePath);
    ui::OnyxLabel* labelFileName = new ui::OnyxLabel(tr("文件名：") + dir.dirName(), this);
    layoutStatus->addWidget(labelFileName);

    ui::OnyxLabel* labelPageCount = new ui::OnyxLabel(tr("总页数：") + QString::number(iPageCount), this);
    layoutStatus->addWidget(labelPageCount);

    ui::OnyxLabel* labelPageNumber = new ui::OnyxLabel(tr("当前页：") + QString::number(iPage+1) + tr("    分页：") + QString::number(iDivPage+1), this);
    layoutStatus->addWidget(labelPageNumber);

    //-- current image
    ui::OnyxLabel* labelCurImg;

    if (profile.imgProfile.option & Opt_ScrollMode)
    {
        labelCurImg = new ui::OnyxLabel(tr("  模式：  卷轴"), this);
    }
    else if (disContent == Cot_Origin)
    {
        labelCurImg = new ui::OnyxLabel(tr("  模式：  原图"), this);
    }
    else if (profile.imgProfile.option & Opt_FitToHeight && profile.imgProfile.option & Opt_FitToWidth)
    {
        labelCurImg = new ui::OnyxLabel(tr("  模式：  最适应缩略图"), this);
    }
    else if (profile.imgProfile.option & Opt_FitToWidth)
    {
        labelCurImg = new ui::OnyxLabel(tr("  模式：  适应宽度缩略图"), this);
    }
    else if (profile.imgProfile.option & Opt_FitToHeight)
    {
        labelCurImg = new ui::OnyxLabel(tr("  模式：  适应高度缩略图"), this);
    }
    
    layoutStatus->addWidget(labelCurImg);

    //-- time
    ui::OnyxLabel* labelTime;
    labelTime = new ui::OnyxLabel(tr("  时间：") + QDateTime::currentDateTime().toString(tr("  h:mm AP")), this);
    layoutStatus->addWidget(labelTime);

    //-- buttons
    m_bOpen = new ui::OnyxPushButton(tr("打开"), this);
    m_buttons.append(m_bOpen);
    layoutButtons->addWidget(m_bOpen);
    connect(m_bOpen, SIGNAL(clicked()), this, SLOT(OnOpen()));

    m_bGoto = new ui::OnyxPushButton(tr("跳转"), this);
    m_buttons.append(m_bGoto);
    layoutButtons->addWidget(m_bGoto);
    connect(m_bGoto, SIGNAL(clicked()), this, SLOT(OnGoto()));

    m_bAddBookmark = new ui::OnyxPushButton(tr("添加书签"), this);
    m_buttons.append(m_bAddBookmark);
    layoutButtons->addWidget(m_bAddBookmark);
    connect(m_bAddBookmark, SIGNAL(clicked()), this, SLOT(OnAddBookmark()));

    m_bRemoveBookmark = new ui::OnyxPushButton(tr("删除书签"), this);
    m_buttons.append(m_bRemoveBookmark);
    layoutButtons->addWidget(m_bRemoveBookmark);
    connect(m_bRemoveBookmark, SIGNAL(clicked()), this, SLOT(OnRemoveBookmark()));

    m_bManageBookmark = new ui::OnyxPushButton(tr("进入书签"), this);
    m_buttons.append(m_bManageBookmark);
    layoutButtons->addWidget(m_bManageBookmark);
    connect(m_bManageBookmark, SIGNAL(clicked()), this, SLOT(OnManageBookmark()));

    m_itrButton = m_buttons.begin();

    //-- timer
    m_timer.setSingleShot(true);
    m_timer.start(300);
}

int StatusDlg::display(CoconProfile& profile, int iPage, int iDivPage, int iPageCount, int disContent,  QWidget* parent)
{
    StatusDlg dlg(profile, iPage, iDivPage, iPageCount, disContent, parent);
    dlg.exec();
    return dlg.m_operation;
}

void StatusDlg::OnBattery()
{
    reject();
    ui::PowerManagementDialog dlg(0, sys::SysStatus::instance());
    dlg.exec();
    g_mainDialog->update();
}

void StatusDlg::OnClock()
{
    reject();
    ui::ClockDialog dlg(QDateTime::currentDateTime());
    dlg.exec();
    g_mainDialog->update();
}

void StatusDlg::OnOpen()
{
    // avoid the repeated key of calling the status diaglog
    if (m_timer.isActive())
    {
        return;
    }

    m_operation = Oper_FileBrowser;
    accept();
}

void StatusDlg::OnGoto()
{
    m_operation = Oper_GotoPage;
    accept();
}

void StatusDlg::OnAddBookmark()
{
    m_operation = Oper_AddBookmark;
    accept();
}

void StatusDlg::OnRemoveBookmark()
{
    m_operation = Oper_RemoveBookmark;
    accept();
}

void StatusDlg::OnManageBookmark()
{
    m_operation = Oper_ManageBookmark;
    accept();
}

bool StatusDlg::event(QEvent* e)
{
    bool ret = ui::OnyxDialog::event(e);

    switch (e->type())
    {
    case QEvent::UpdateRequest:
        {
            onyx::screen::ScreenProxy::instance().flush(this, ScreenProxy::GU, false);
            return true;
        }
        break;
    default:
        {

        }
        break;
    }
    return ret;
}

void StatusDlg::keyPressEvent(QKeyEvent* event)
{
    switch (event->key())
    {
    case Qt::Key_Up:
    case Qt::Key_Left:
    case Qt::Key_PageUp:
        {
            if (m_itrButton != m_buttons.begin())
            {
                --m_itrButton;
            }
            else
            {
                m_itrButton = m_buttons.end() - 1;
            }
            (*m_itrButton)->setFocus();
        }
        break;

    case Qt::Key_Down:
    case Qt::Key_Right:
    case Qt::Key_PageDown:
        {
            if (m_itrButton + 1 != m_buttons.end())
            {
                ++m_itrButton;
            }
            else
            {
                m_itrButton = m_buttons.begin();
            }
            (*m_itrButton)->setFocus();
        }
        break;
    default:
        break;
    }
}
